<template>
  <van-form @submit="check">
    <!-- readonly -->
    <van-field
      v-model="business.email"
      name="email"
      label="邮箱"
      placeholder="请输入邮箱"
      readonly
      :rules="[
        {required: true, message: '请输入邮箱'},
        {pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, message: '邮箱格式不正确'}
      ]"
    />

    <van-field
      v-model="vercode"
      name="vercode"
      center
      clearable
      label="短信验证码"
      placeholder="请输入短信验证码"
      :rules="[
        {required: true, message: '请输入短信验证码'}
      ]"
    >
      <template #button>
        <van-button size="small" type="primary" @click="send" :disabled="sendis">{{msg}}</van-button>
      </template>
    </van-field>

    <div style="width:50%;margin:2em auto;">
      <van-button round block type="primary" native-type="submit" :disabled = "subdis">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
  const {proxy} = getCurrentInstance()
  let business = proxy.$business
  let vercode = ref('')
  let msg = ref('发送验证码')
  let sendis = ref(false)
  let subdis = ref(false)

  //渲染前的钩子函数生命周期
  onBeforeMount( () => {
    if(!business.value.email)
    { 
      sendis.value = true
      subdis.value = true
      proxy.$confirm({
            title: '你的邮箱尚未填写',
            message: "是否设置邮箱",
            confirmButtonText: '是',
            cancelButtonText: '否',
        }).then(async () => {
            proxy.$router.push('/business/profile')
            
        }).catch(() => proxy.$router.push('/business'))

      // proxy.$fail({
      //   message: '请先去基本资料填写邮箱',
      //   onClose:() =>
      //   {
      //     proxy.$router.back();
      //   }
      // })
      // return false;
    }

    if(business.value.auth == 1)
    {
      sendis.value = true
      subdis.value = true
      proxy.$success({
        message:'您已验证，无须重复验证',
        // duration: 500,
        onClose:() =>
        {
          proxy.$router.go(-1);
        }
      })
    }
  })

  //邮箱发送
  const send = async () => 
  {
    let result = await proxy.$POST({
      url: 'business/email',
      params:{
        busid: business.value.id,
        email: business.value.email,
        action: 'send'
      }
    })
    msg.value = '正在发送中.....'
    sendis.value = true
    msg.value = 60

    var T = setInterval(() => {
      if(msg.value <= 0)
      {
        //停止定时器
        clearInterval(T)
        msg.value = '重新发送验证码'
        sendis.value = false
      }else
      {
        msg.value--
      }
    }, 100)

  }

  //邮箱验证
  const check = async (values) =>
  {
    let result = await proxy.$POST({
      url: 'business/email',
      params: {
        busid: business.value.id,
        email: business.value.email,
        action: 'check',
        vercode: vercode.value
      }
    })

    if(result.code == 0)
    {
      proxy.$fail(result.msg)
      return false
    }
    proxy.$success({
      message: result.msg,
      duration: 1000,
      onClose: () => 
      {
        proxy.$router.go(-1)
      }
    })
    console.log('提交表单')
  }
</script>
